﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using Microsoft.SharePoint;
using System.Configuration;


namespace SCCL2010
{
    public static class ConfigWeb
    {


        ///// <summary>
        ///// Method to retrieve the ConnectionString based on the ConfigName.
        ///// </summary>
        ///// <param name="oWorkWeb"></param>
        ///// <param name="cConfigName"></param>
        ///// <returns>retuns a string which contains ConnectionString.</returns>
        //public static string GetConnectionString(SPWeb oWorkWeb, string cConfigName)
        //{
        //    string cConnectStr = "";
        //    try
        //    {
        //        string cSiteUrl = oWorkWeb.Site.Url.ToString();
        //        SPSecurity.RunWithElevatedPrivileges(() =>
        //        {
        //            using (SPSite oSite = new SPSite(cSiteUrl))
        //            {
        //                try
        //                {
        //                    SPWebApplication webApplication = oSite.WebApplication;
        //                    Configuration wconfig = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);

        //                    if (HasConnectionString(cConfigName, wconfig))
        //                        cConnectStr = wconfig.ConnectionStrings.ConnectionStrings[cConfigName].ConnectionString;
        //                }
        //                catch (Exception ex)
        //                {
        //                    ErrorHandling.HandleError(oSite.RootWeb, "ConfigWeb.GetConnectionString", ex);
        //                }
        //            }
        //        });
        //    }
        //    catch (Exception ex)
        //    {
        //        ErrorHandling.HandleError(oWorkWeb, "ConfigWeb.GetConnectionString", ex);
        //    }
        //    return cConnectStr;
        //}

        ///// <summary>
        ///// Method to check if the webconfig has a value ConnectionString based on the input parameter.
        ///// </summary>
        ///// <param name="cConnectStr"></param>
        ///// <param name="webconfig"></param>
        ///// <returns>returns a boolean value representing if the webconfig has a connectionstring or not.</returns>
        //private static bool HasConnectionString(string cConnectStr, Configuration webconfig)
        //{
        //    try
        //    {
        //        foreach (ConnectionStringSettings item in webconfig.ConnectionStrings.ConnectionStrings)
        //        {
        //            if (item.Name == cConnectStr)
        //                return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        ErrorHandling.HandleError(oWeb, "ConfigWeb.HasConnectionString", ex);
        //    }
        //    return false;
        //}

    }
}
